local extension = Package:new("wq_yizhi")
extension.extensionName = "FkWqmt"
Fk:loadTranslationTable{
  ["wq_yizhi"] = "无期迷途·异质",
}

local Wqc = require "packages/FkWqmt/wq_common"
local U = require "packages/utility/utility"

-- 禁闭者：夏音
local wq_xiayin = General:new(extension, "wq_xiayin", "qun", 3, 3, General.Female)
-- 技能：异造
local wq_skill_yizao = fk.CreateTriggerSkill{
  name = "wq_skill_yizao",
  anim_type = "special",
  events = {fk.EventPhaseStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.AfterCardsMove then
        if player.phase == Player.Discard then
          local n = 0
          for _, move in ipairs(data) do
            if move.from == player.id and move.moveReason == fk.ReasonDiscard then
              for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                  n = n + 1
                end
              end
            end
          end
          if n > 0 then
            return true
          end
        end
      elseif event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Start
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      player:drawCards(1, self.name)
      if not player:isKongcheng() then
        local card = room:askForCard(player, 1, 1, false, self.name, false, nil, "#wq_skill_yizao_prompt")
        player:addToPile("wq_pile_pao", card, true, self.name)
      end
    else
      local cards = {}
      for _, move in ipairs(data) do
        if move.from == player.id and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and
            room:getCardArea(info.cardId) == Card.DiscardPile then
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
      end
      if #cards > 0 then
        local dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(cards)
        player:addToPile("wq_pile_pao", dummy, true, self.name)
      end
    end
  end,
}
local wq_skill_yizao_TargetMod = fk.CreateTargetModSkill{
  name = "#wq_skill_yizao_TargetMod",
  frequency = Skill.Compulsory,
  residue_func = function(self, player, skill, scope, card)
    return (player:hasSkill("wq_skill_yizao") and skill.trueName == "slash_skill") and #player:getPile("wq_pile_pao") or 0
  end,
}
local wq_skill_yizao_attackrange = fk.CreateAttackRangeSkill{
  name = "#wq_skill_yizao_attackrange",
  frequency = Skill.Compulsory,
  correct_func = function (self, from, to)
    if from:hasSkill("wq_skill_yizao") and #from:getPile("wq_pile_pao") > 0 then
      return #from:getPile("wq_pile_pao")
    end
    return 0
  end,
}
wq_skill_yizao:addRelatedSkill(wq_skill_yizao_TargetMod)
wq_skill_yizao:addRelatedSkill(wq_skill_yizao_attackrange)
-- 技能：创生
local wq_skill_chuangsheng = fk.CreateTriggerSkill{
  name = "wq_skill_chuangsheng",
  anim_type = "offensive",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and Wqc.getAllPileSize(player) > 0 and data.card and data.card.trueName == "slash" and not data.chain
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#wq_skill_chuangsheng_prompt")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = Wqc.getAllPileSize(player)
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|1~"..num.."|.",
    }
    room:judge(judge)
    if judge.card.number <= num then
      data.damage = data.damage + 1
    end
  end,
  refresh_events = {fk.FinishJudge},
  can_refresh = function(self, event, target, player, data)
    local room = player.room
    return player:hasSkill(self) and data.who.id == player.id and data.reason == self.name 
    and room:getCardArea(data.card) == Card.Processing and data.card.number > Wqc.getAllPileSize(player)
  end,
  on_refresh = function(self, event, target, player, data)
    player:addToPile("wq_pile_pao", data.card, true, self.name)
  end,
}
-- 技能：超载
local wq_skill_chaozai = fk.CreateActiveSkill{
  name = "wq_skill_chaozai",
  anim_type = "drawCards",
  frequency = Skill.Limited,
  card_num = 0,
  target_num = 0,
  prompt = function (self, selected, selected_cards)
    local num = math.min(#Self:getPile("wq_pile_pao"), 5)
    return "#wq_skill_chaozai_prompt:::"..num
  end,
  can_use = function(self, player)
    return #player:getPile("wq_pile_pao") > 0 and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local num = math.min(#player:getPile("wq_pile_pao"), 5)
    room:drawCards(player, num, self.name)
    local cids = room:askForExchange(player, {player:getPile("wq_pile_pao"), player:getCardIds(Player.Hand)}, {"wq_pile_pao", "$Hand"}, self.name)
    local cards1, cards2 = {}, {}
    for _, id in ipairs(cids[1]) do
      if room:getCardArea(id) == Player.Hand then
        table.insert(cards1, id)
      end
    end
    for _, id in ipairs(cids[2]) do
      if room:getCardArea(id) ~= Player.Hand then
        table.insert(cards2, id)
      end
    end
    room:moveCards(
      {
      ids = cards2,
        from = player.id,
        to = player.id,
        fromArea = Card.PlayerSpecial,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonExchange,
        proposer = player.id,
        skillName = self.name,
      },
      {
      ids = cards1,
        from = player.id,
        to = player.id,
        fromArea = Card.PlayerHand,
        toArea = Card.PlayerSpecial,
        moveReason = fk.ReasonExchange,
        proposer = player.id,
        specialName = "wq_pile_pao",
        skillName = self.name,
      }
    )
  end,
}
-- 夏音技能
wq_xiayin:addSkill(wq_skill_yizao)
wq_xiayin:addSkill(wq_skill_chuangsheng)
wq_xiayin:addSkill(wq_skill_chaozai)
-- 夏音翻译
Fk:loadTranslationTable{
  ["wq_xiayin"] = "夏音",
  ["#wq_xiayin"] = "无限知能",
  ["designer:wq_xiayin"] = "妙霄",
  ["cv:wq_xiayin"] = "官方",
  ["illustrator:wq_xiayin"] = "官方",
  ["wq_skill_yizao"] = "异造",
  [":wq_skill_yizao"] = "准备阶段，你可以摸一张牌，然后将一张手牌置于武将牌上；弃牌阶段，你可以将此阶段内你弃置的牌置于武将牌上。你以此法放置的牌称为「炮」。锁定技，你的攻击范围+X，你使用【杀】的限制次数+X（X为「炮」的数量）。",
  ["$wq_skill_yizao"] = "看好了哦，这是环武器。",
  ["#wq_skill_yizao_prompt"] = "异造：请将一张手牌置于武将牌上称为「炮」",
  ["wq_pile_pao"] = "炮",
  ["wq_skill_chuangsheng"] = "创生",
  [":wq_skill_chuangsheng"] = "当你使用【杀】造成伤害时，你可以判定。若判定牌的点数不大于你武将牌上牌的数量，则此伤害+1；否则你将判定牌置于武将牌上，称为「炮」。",
  ["$wq_skill_chuangsheng"] = "全机锁定，区域净空！",
  ["#wq_skill_chuangsheng_prompt"] = "创生：你可以判定，若判定牌的点数不大于你武将牌上牌的数量则此伤害+1，否则你将判定牌置于武将牌上称为「炮」",
  ["wq_skill_chaozai"] = "超载",
  [":wq_skill_chaozai"] = "限定技，出牌阶段，你可以摸X张牌，然后用任意数量的手牌等量交换武将牌上的「炮」（X为「炮」的数量且至多为5）。",
  ["$wq_skill_chaozai1"] = "英——雄——登——场！",
  ["$wq_skill_chaozai2"] = "邪恶，终究会被打败。",
  ["#wq_skill_chaozai_prompt"] = "超载：你可以摸 %arg 张牌，然后用任意数量的手牌等量交换武将牌上的「炮」",
  ["~wq_xiayin"] = "呜……我还……没毕业呢……",
}

-- 禁闭者：观星者
local wq_guanxingzhe = General:new(extension, "wq_guanxingzhe", "qun", 3, 3, General.Female)
-- 技能：明星
local wq_skill_mingxing = fk.CreateActiveSkill{
  name = "wq_skill_mingxing",
  anim_type = "control",
  card_num = 0,
  target_num = 2,
  prompt = function (self, selected, selected_cards)
    return "#wq_skill_mingxing_prompt-play"
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, to_select, selected)
    return false
  end,
  target_filter = function(self, to_select, selected)
    local room = Fk:currentRoom()
    if #selected == 0 then
      return #room:getPlayerById(to_select):getPile("wq_pile_xing") > 0
    else
      return true
    end
  end,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target_from = room:getPlayerById(effect.tos[1])
    local target_to = room:getPlayerById(effect.tos[2])
    local cids = target_from:getPile("wq_pile_xing")
    local cid = nil
    if #cids > 1 then
      room:fillAG(player, cids)
      cid = room:askForAG(player, cids, false, self.name)
      room:closeAG(player)
    elseif #cids == 1 then
      cid = cids[1]
    end
    if cid ~= nil then
      target_to:addToPile("wq_pile_xing", cid, true, self.name)
    end
  end,
}
local wq_skill_mingxing_buff = fk.CreateTriggerSkill{
  name = "#wq_skill_mingxing_buff",
  events = {fk.GameStart, fk.EnterDying},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill(self)
    elseif event == fk.EnterDying then
      return player:hasSkill(self) and target.dying and #target:getPile("wq_pile_xing") > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      return true
    elseif event == fk.EnterDying then
      local to = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(target), Util.IdMapper), 1, 1, "#wq_skill_mingxing_prompt-dying:"..target.id, "wq_skill_mingxing", true)
      if #to > 0 then
        self.cost_data = to[1]
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("wq_skill_mingxing")
    room:notifySkillInvoked(player, "wq_skill_mingxing")
    if event == fk.GameStart then
      room:drawCards(player, 2, "wq_skill_mingxing")
      for i = 1, 2, 1 do
        if player:isAlive() and (not player:isKongcheng()) then
          local tids, cids = U.askForChooseCardsAndPlayers(room, player, 1, 1, table.map(room:getAlivePlayers(), function(p) return p.id end), 1, 1, ".|.|.|hand", "#wq_skill_mingxing_prompt-gameStart", "wq_skill_mingxing", false)
          room:getPlayerById(tids[1]):addToPile("wq_pile_xing", cids[1], true, "wq_skill_mingxing")
        end
      end
    elseif event == fk.EnterDying then
      local target_from = target
      local target_to = room:getPlayerById(self.cost_data)
      local cids = target_from:getPile("wq_pile_xing")
      local cid = nil
      if #cids > 1 then
        room:fillAG(player, cids)
        cid = room:askForAG(player, cids, false, self.name)
        room:closeAG(player)
      elseif #cids == 1 then
        cid = cids[1]
      end
      if cid ~= nil then
        target_to:addToPile("wq_pile_xing", cid, true, self.name)
      end
    end
  end,
}
wq_skill_mingxing:addRelatedSkill(wq_skill_mingxing_buff)
-- 技能：星引
local wq_skill_xingyin = fk.CreateTriggerSkill{
  name = "wq_skill_xingyin",
  anim_type = "offensive",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(self) and (not target:isNude()) and #target:getPile("wq_pile_xing") > 0 and
    (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) and
    #table.filter(target:getPile("wq_pile_xing"), function(cid) return data.card.color == Fk:getCardById(cid).color end) > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_xingyin_prompt:"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local id = room:askForCardChosen(player, target, "he", self.name)
    room:throwCard(id, self.name, target, player)
  end,
}
-- 技能：梦境
local wq_skill_mengjing = fk.CreateActiveSkill{
  name = "wq_skill_mengjing",
  anim_type = "offensive",
  frequency = Skill.Limited,
  card_num = 0,
  target_num = 0,
  prompt = function (self, selected, selected_cards)
    return "#wq_skill_mengjing_prompt"
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if player:isAlive() and p:isAlive() and #p:getPile("wq_pile_xing") > 0 then
        room:damage{
          from = player,
          to = p,
          damage = 1,
          skillName = self.name,
        }
      end
    end
  end,
}
-- 观星者的技能
wq_guanxingzhe:addSkill(wq_skill_mingxing)
wq_guanxingzhe:addSkill(wq_skill_xingyin)
wq_guanxingzhe:addSkill(wq_skill_mengjing)
-- 观星者的翻译
Fk:loadTranslationTable{
  ["wq_guanxingzhe"] = "观星者",
  ["#wq_guanxingzhe"] = "蚀相降临",
  ["designer:wq_guanxingzhe"] = "妙霄",
  ["cv:wq_guanxingzhe"] = "官方",
  ["illustrator:wq_guanxingzhe"] = "官方",
  ["wq_skill_mingxing"] = "明星",
  [":wq_skill_mingxing"] = "游戏开始时，你摸两张牌，然后你需将两张手牌依次置于一名角色的武将牌上，称为「星」。出牌阶段限一次/一名角色进入濒死状态时，你可以移动场上/其武将牌上的一张「星」。",
  ["$wq_skill_mingxing"] = "我正在与灵魂交流。",
  ["#wq_skill_mingxing_prompt-play"] = "明星：你可以移动场上的一张「星」",
  ["#wq_skill_mingxing_prompt-dying"] = "明星：你可以将%src武将牌上的一张「星」移动给另一名角色",
  ["#wq_skill_mingxing_prompt-gameStart"] = "明星：请将一张手牌置于一名角色的武将牌上称为「星」",
  ["wq_pile_xing"] = "星",
  ["wq_skill_xingyin"] = "星引",
  [":wq_skill_xingyin"] = "当其他角色使用基本牌或普通锦囊牌时，若其武将牌上有与此牌颜色相同的「星」，你可以弃置其一张牌。",
  ["$wq_skill_xingyin"] = "我会洞悉你的一切……看穿你的灵魂！",
  ["#wq_skill_xingyin_prompt"] = "星引：你可以弃置%src的一张牌",
  ["wq_skill_mengjing"] = "梦境",
  [":wq_skill_mengjing"] = "限定技，出牌阶段，你可以对所有武将牌上有「星」的其他角色造成1点伤害。",
  ["$wq_skill_mengjing1"] = "白色的沙漠就像纯粹的梦幻之地，绽放出无瑕的花儿来……",
  ["$wq_skill_mengjing2"] = "我曾在梦中造访过真实之境，我希望……有一天你也能出现在那里。",
  ["#wq_skill_mengjing_prompt"] = "梦境：你可以对所有武将牌上有「星」的其他角色造成1点伤害",
  ["~wq_guanxingzhe"] = "我的灵魂终于……得到解放……",
}

return extension